package language.rmi.simple;

import org.junit.Test;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class Server {
    public static void main(String[] args) throws RemoteException {
        Hello hello = new Helloimpl();
        // 在localhost 1199端口开启了一个RMI服务
        Registry registry = LocateRegistry.createRegistry(1199);
        // 绑定对象到注册表，并给他取名为hello
        registry.rebind("hello", hello);
        System.out.println("hello bound");
    }

    @Test
    public void client() throws RemoteException, NotBoundException {
        // 获取到Registry的代理
        Registry registry = LocateRegistry.getRegistry("localhost", 1199);
        // 利用注册表的代理去查询远程注册表中名为hello的对象
        Hello hello = (Hello) registry.lookup("hello");
        // 调用远程方法
        System.out.println(hello.welcome("axin"));
    }
}